﻿
package extremefx.i18n {

	internal class GregorianEra {
		
		private var _nr:int;
		
		/**
		 * This integer gives the first day of the era as fixed day number.
		 */
		private var _start:int;
		
		/**
		 * his integer gives the gregorian year of the <code>_start</code> value.
		 */
		private var _gregorianYearStart:int;
		
		/**
		 * This integer gives the last day of the era as fixed day number.
		 */
		private var _end:int;
		
		/**
		 * This integer gives the largest year number of this era.
		 */
		private var _maxYear:int;
		
		public function GregorianEra(pNr:int, pStart:int, pEnd:int) {
			if (pNr == 0) throw new ArgumentError("Era number shouldn't be zero.");
			
			_nr = pNr;
			
			if (pStart > pEnd) throw new ArgumentError("Era should start before end.");

			_start = pStart;
			_end = pEnd;

			_gregorianYearStart = CCGregorian._yearFromFixed(_start);
			_maxYear = CCGregorian._yearFromFixed(_end) - _gregorianYearStart + 1;
		}
		
		internal function gregorianYear(pYear:int):int {
			if (pYear < 1 || pYear > _maxYear) throw new RangeError("pDate", XString.format("Valid Values are between {0} and {1}, inclusive.", 1, _maxYear));
			return pYear + _gregorianYearStart - 1;
		}
		
		internal function covers(pDate:int):Boolean {
			return _start <= pDate && pDate <= _end;
		}
		
		/**
		 * @return an array containing [year of the era, era number]
		 */
		internal function eraYear(pEra:Array, pDate:int):int {
			if (!covers(pDate)) throw new RangeError("pDate", "Time was out of Era range.");
			pEra[0] = _nr;
			return CCGregorian._yearFromFixed(pDate) - _gregorianYearStart + 1;
		}
		
		/**
		 * A get-only property that gives the era integer number.
		 */
		internal function get nr():int { return _nr; }
	}
}
